package com.hybao.topApi;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hybao.common.TopBaseClient;
import com.taobao.api.ApiException;
import com.taobao.api.domain.GradePromotion;
import com.taobao.api.request.CrmGradeGetRequest;
import com.taobao.api.request.CrmMembersGetRequest;
import com.taobao.api.response.CrmGradeGetResponse;
import com.taobao.api.response.CrmMembersGetResponse;

public class CrmTopService extends TopBaseClient{
	public List<GradePromotion> getCrmGrade(String sessionKey){
		CrmGradeGetRequest req = new CrmGradeGetRequest();
		CrmGradeGetResponse response = null;
		try {
			response = this.getClient().execute(req , sessionKey);
		} catch (ApiException e) {
			this.getLog().error("crmGrade top get error",e);
		}
		if(response!= null && response.isSuccess()){
			return response.getGradePromotions();
		}
		return null;
	}
	
	public Map<String,String> getCrmGradeMembers(String sessionKey){
		Map<String,String> resMap = new HashMap<String,String>();
		resMap.put("1", getGradeResults(1L,sessionKey));
		resMap.put("2", getGradeResults(2L,sessionKey));
		resMap.put("3", getGradeResults(3L,sessionKey));
		resMap.put("4", getGradeResults(4L,sessionKey));
		return resMap;
	}
	
	private String getGradeResults(Long grade, String sessionKey){
		CrmMembersGetRequest req=new CrmMembersGetRequest();
		req.setPageSize(100L);
		req.setCurrentPage(1L);
		CrmMembersGetResponse response = null;
		req.setGrade(grade);
		try {
			response = this.getClient().execute(req , sessionKey);
		} catch (ApiException e) {
			this.getLog().error("crmGrade top get error",e);
		}
		if(response!= null && response.isSuccess()){
			return response.getTotalResult() + "";
		}
		else{
			return "0";
		}
	}
}
